<template>
  <div class="search">
    <!-- 搜索组件一级路由   $router.back()返回上一个页面-->
    <van-nav-bar
      left-arrow
      title="搜索中心"
      @click-left="$router.back()"
    />

    <!-- https://youzan.github.io/vant/#/zh-CN/search -->
    <van-search
      v-model.trim="keyword"
      show-action
      placeholder="请输入搜索关键词"
      shape="round"
      @input="getSuggestion"
    >
      <template #action>
        <div @click="clickSearch">搜索</div>
      </template>
    </van-search>

    <!-- 联想建议 -->
    <van-cell-group v-if="suggestion.length">
      <van-cell
        v-for="(item,index) in showSuggestion"
        :key="index"
        icon="search"
        @click="clickSuggestion(index)"
      >
        <div v-html="item" />
      </van-cell>
    </van-cell-group>
    <!-- /联想建议 -->

    <!-- 搜索历史记录 -->
    <van-cell-group v-else>
      <van-cell title="历史记录" />
      <van-cell v-for="(item,index) in history" :key="item" :title="item" @click="clickHistory(item)">
        <van-icon name="close" @click.stop="delHistory(index)" />
      </van-cell>

    </van-cell-group>
    <!-- /搜索历史记录 -->
  </div>
</template>

<script>
import { reqGetSuggestion } from '@/api/search.js'
import { setHistory, getHistory } from '@/utils/storage.js'
export default {
  name: 'Search',
  data() {
    return {
      keyword: '', // 搜索关键字
      suggestion: [],
      history: getHistory(),
      timeId: null
    }
  },

  computed: {
    showSuggestion() {
      const regExp = new RegExp(this.keyword, 'ig')
      const arr = this.suggestion.map(item => {
        const newStr = item.replace(regExp, (match) => {
          return `<span style='color :red'>${match} </span>`
        })
        // 连接字符串
        return newStr
      })
      // 返回结果
      return arr
    }
  },
  created() {
    this.history = getHistory()
  },
  methods: {
    // 进行防抖处理
    getSuggestion() {
      clearTimeout(this.timeId)
      this.timeId = setTimeout(async() => {
        if (this.keyword === '') {
          this.suggestion = []
          return
        }
        const { data: { options }} = await reqGetSuggestion(this.keyword)
        this.suggestion = options
      }, 300)
    },

    clickSearch() {
      if (this.keyword === '') return
      this.addHistory(this.keyword)
    },

    clickSuggestion(index) {
      this.addHistory(this.suggestion[index])
    },

    addHistory(str) {
      const index = this.history.findIndex(item => item === str)
      if (index !== -1) {
        this.history.splice(index, 1)
      }
      this.history.unshift(str)
      setHistory(this.history)
      this.$router.push({
        path: '/searchResult',
        query: {
          keyword: str
        }
      })
    },

    delHistory(index) {
      this.history.splice(index, 1)
      setHistory(this.history)
    },
    clickHistory(value) {
      this.addHistory(value)
      this.$router.push({
        path: '/searchResult',
        query: {
          keyword: value
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .search {
    ::v-deep{
      // 控制头部导航栏的通用样式
      .van-nav-bar {
        background-color: #3196fa;
        .van-nav-bar__title {
          color: white;
        }
        .van-icon {
          color: white;
        }
      }
    }
  }
</style>
